package day5;

public class demo3 {
    public static void main(String[] args) {
        //二分查找
        int[] nums = {1,2,3,4,5,6,7,8,9,10};
        int number=5;
        int index=search(nums,number);
        System.out.println("下标："+index);
    }
    public static int search(int[] nums, int number) {
        int min=0,max=nums.length-1;
        while (min<=max) {
            int mid=(max+min)/2;
            if (nums[mid]==number) {
                return mid;
            }
            if (nums[mid]<number) {
                min=mid+1;
            }
            if (nums[mid]>number) {
                max=mid-1;
            }
        }
        return -1;
    }
}
